Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Sorts any JavaScript array in a predictable way (deep equal arrays are always sorted in the same order)
JS library which always sorts arrays in a predictable way. Moreover in contrary to Array.prototype.sort
, it does not modify the argument.
Array.prototype.sort
:
[[[11]],[[2]],2,{foo:{foo:2}},1,4,2,{foo:{foo:1}},32,[3,4],[1,2],{foo:1},{foo:0},{foo:{}}].sort()
/*
It gives:
[ 1,
[ 1, 2 ],
[ [ 11 ] ],
2,
2,
[ [ 2 ] ],
[ 3, 4 ],
32,
4,
{ foo: { foo: 2 } },
{ foo: {} },
{ foo: 1 },
{ foo: 0 },
{ foo: { foo: 1 } } ]
*/
The same array in a different order:
[[[11]],[[2]],2,1,4,2,{foo:{foo:1}},32,[3,4],[1,2],{foo:0},{foo:1},{foo:{}},{foo:{foo:2}}].sort()
/*
It gives:
[ 1,
[ 1, 2 ],
[ [ 11 ] ],
2,
2,
[ [ 2 ] ],
[ 3, 4 ],
32,
4,
{ foo: { foo: 1 } },
{ foo: 0 },
{ foo: 1 },
{ foo: {} },
{ foo: { foo: 2 } } ]
*/
So the results of Array.prototype.sort
are strange (eg. numbers are sorted in the alphabetical order) and moreover if we change the array order (eg. for object items), the result has order changed as well.
So I have implemented this library to work like that:
const sort = require('sort-any');
sort([[[11]],[[2]],2,{foo:{foo:2}},1,4,2,{foo:{foo:1}},32,[3,4],[1,2],{foo:1},{foo:0},{foo:{}}])
/*
It returns:
[ 1,
2,
2,
4,
32,
[ [ 2 ] ],
[ [ 11 ] ],
[ 1, 2 ],
[ 3, 4 ],
{ foo: 0 },
{ foo: 1 },
{ foo: {} },
{ foo: { foo: 1 } },
{ foo: { foo: 2 } } ]
*/
And when we change the order, the result remains the same.
const sort = require('sort-any');
sort([[[11]],[[2]],2,1,4,2,{foo:{foo:1}},32,[3,4],[1,2],{foo:0},{foo:1},{foo:{}},{foo:{foo:2}}])
/*
It returns:
[ 1,
2,
2,
4,
32,
[ [ 2 ] ],
[ [ 11 ] ],
[ 1, 2 ],
[ 3, 4 ],
{ foo: 0 },
{ foo: 1 },
{ foo: {} },
{ foo: { foo: 1 } },
{ foo: { foo: 2 } } ]
*/
Rules for sorting:
false
is less than true
-Infinity
is less than any other numberInfinity
is more than any other numberObject.keys(object)[0]
)FAQs
Sorts any JavaScript array in a predictable way (deep equal arrays are always sorted in the same order)
The npm package sort-any receives a total of 203,288 weekly downloads. As such, sort-any popularity was classified as popular.
We found that sort-any demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.